package com.ruoyi.crop.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.crop.api.domain.Farmer;
import com.ruoyi.crop.api.domain.GoodsProduct;
import com.ruoyi.crop.api.domain.MaterialLot;
import com.ruoyi.crop.api.domain.Product;
import com.ruoyi.crop.mapper.GoodsProductMapper;
import com.ruoyi.crop.service.ProductService;
import com.ruoyi.crop.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author 86178
* @description 针对表【src_product(产品表)】的数据库操作Service实现
* @createDate 2024-07-27 09:35:55
*/
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
    implements ProductService{
    @Autowired
    GoodsProductMapper goodsProductMapper;

    @Override
    public List<Product> selectProductList(Product product) {
        return baseMapper.selectProductList(product);
    }

    @Override
    public List<Product> selectDelFlagList(Product product) {
        return baseMapper.selectDelFlagList(product);
    }

    @Override
    public List<Farmer> selectFarmerList() {
        return baseMapper.selectFarmerList();
    }
    @Override
    public List<MaterialLot> selectMaterialLotList() {
        return baseMapper.selectMaterialLotList();
    }

    @Override
    @Transactional
    public int saveAll(Product product) {
        int insert = baseMapper.insert(product);
        Integer[] gids = product.getGids();
        if(gids.length!=0 && gids!=null){
            for (Integer gid : gids) {
                GoodsProduct goodsProduct = new GoodsProduct();
                goodsProduct.setProductId(product.getProductId());
                goodsProduct.setGoodsId(gid);
                goodsProductMapper.insert(goodsProduct);
            }
        }
        return insert;
    }

    @Override
    public List<Integer> selectProductByGids(Integer productId) {
        return baseMapper.selectProductByGids(productId);
    }

    @Override
    public int updateProduct(Product product) {
        int i = baseMapper.updateById(product);
        goodsProductMapper.delete(Wrappers.lambdaQuery(GoodsProduct.class).eq(GoodsProduct::getProductId,product.getProductId()));
        Integer[] gids = product.getGids();
        if(gids.length!=0 && gids!=null){
            for (Integer gid : gids) {
                GoodsProduct goodsProduct = new GoodsProduct();
                goodsProduct.setProductId(product.getProductId());
                goodsProduct.setGoodsId(gid);
                goodsProductMapper.insert(goodsProduct);
            }
        }
        return i;
    }

    @Override
    public int removeDelFlagIds(List<Integer> productIds) {
        return baseMapper.removeDelFlagIds(productIds);
    }

    @Override
    public int delProductById(List<Integer> productIds) {
        return baseMapper.delProductById(productIds);
    }
}




